/** Sopaco - (c) Sopaco Advanced Technique Support */
import React, { Component } from 'react';
import { Provider } from 'mobx-react';
import { Schedule } from 'react-apm-api';
import { ConsoleOutlet, MemoryRecorder, Profiler } from 'react-apm-core';
import { BootPlugin, CpuPlugin, FpsPlugin, GcpPlugin, JSEventLoopPlugin, MemoryPlugin, MethodTracePlugin, MrtPlugin, ScuPlugin, TrafficPlugin } from 'react-apm-plugins';

function assemble(Comp, contextCreator, convert) {
    return class MobxComponent extends Component {
        constructor(props) {
            super(props);
            this.state = { params: null };
            const params = convert ? convert(props) : props.params;
            this.contexture = contextCreator(params);
        }
        static getDerivedStateFromProps(nextProps, prevState) {
            const params = convert ? convert(nextProps) : {};
            return { params };
        }
        render() {
            const { contexture, state } = this;
            return (React.createElement(Provider, { context: contexture },
                React.createElement(Comp, Object.assign({}, this.props, { context: contexture, params: state.params }))));
        }
        componentDidMount() {
            const { contexture } = this;
            return contexture.componentDidMount && contexture.componentDidMount();
        }
        getSnapshotBeforeUpdate(prevProps, prevState) {
            const { contexture } = this;
            return contexture.getSnapshotBeforeUpdate && contexture.getSnapshotBeforeUpdate(prevProps, prevState);
        }
        componentDidUpdate() {
            const { contexture } = this;
            return contexture.componentDidUpdate && contexture.componentDidUpdate();
        }
        componentWillUnmount() {
            const { contexture } = this;
            return contexture.componentWillUnmount && contexture.componentWillUnmount();
        }
        componentDidCatch(error, info) {
            const { contexture } = this;
            return contexture.componentDidCatch && contexture.componentDidCatch(error, info);
        }
    };
}

class Context {
    constructor(props) {
        this.props = props;
    }
}



var index = Object.freeze({
	assemble: assemble,
	Context: Context
});

function launchDefaults() {
    return Promise.all([
        CpuPlugin,
        FpsPlugin,
        GcpPlugin,
        MemoryPlugin,
        MrtPlugin,
        TrafficPlugin,
        ScuPlugin,
        BootPlugin,
        JSEventLoopPlugin,
        MethodTracePlugin
    ].map(PluginType => {
        const plugin = new PluginType();
        const recorder = new MemoryRecorder();
        const outlet = new ConsoleOutlet();
        const profiler = new Profiler(plugin, recorder, outlet);
        return profiler.start({ boot: Schedule.Erupt, sampler: {} });
    }));
}


var index$1 = Object.freeze({
	launchDefaults: launchDefaults
});

export { index as mobx, index$1 as performance };
